import { Model, DataTypes, Optional } from 'sequelize';
import sequelize from '@/config/database';

// 定义角色属性接口
interface RoleAttributes {
  roleId: number;
  roleName: string;
  roleKey: string;
  roleSort: number;
  dataScope?: string;
  menuCheckStrictly?: boolean;
  deptCheckStrictly?: boolean;
  status: string;
  delFlag?: string;
  createBy?: string;
  createTime?: Date;
  updateBy?: string;
  updateTime?: Date;
  remark?: string;
  flag?: boolean; // 添加flag属性，用于前端显示
  menuIds?: number[] | null; // 菜单ID列表
  deptIds?: number[] | null; // 部门ID列表
  permissions?: string[] | null; // 权限列表
  admin?: boolean; // 是否为管理员
}

// 创建时可选的字段
interface RoleCreationAttributes extends Optional<RoleAttributes, 'roleId'> {}

// 角色模型类
class Role extends Model<RoleAttributes, RoleCreationAttributes> implements RoleAttributes {
  public roleId!: number;
  public roleName!: string;
  public roleKey!: string;
  public roleSort!: number;
  public dataScope?: string;
  public menuCheckStrictly?: boolean;
  public deptCheckStrictly?: boolean;
  public status!: string;
  public delFlag?: string;
  public createBy?: string;
  public createTime?: Date;
  public updateBy?: string;
  public updateTime?: Date;
  public remark?: string;
  public flag?: boolean;
  public menuIds?: number[] | null;
  public deptIds?: number[] | null;
  public permissions?: string[] | null;
  public admin?: boolean;

  // 时间戳
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  // 检查角色状态是否正常
  public isActive(): boolean {
    return this.status === '0' && (this.delFlag === '0' || this.delFlag === undefined);
  }

  // 检查是否为管理员角色
  public isAdmin(): boolean {
    return this.roleKey === 'admin';
  }

  // 获取数据权限范围
  public getDataScope(): string {
    return this.dataScope || '1'; // 默认为全部数据权限
  }
}

Role.init(
  {
    roleId: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      autoIncrement: true,
      field: 'role_id',
      get() {
        // 确保返回数字类型
        const value = this.getDataValue('roleId');
        return value !== undefined ? Number(value) : undefined;
      }
    },
    roleName: {
      type: DataTypes.STRING(30),
      allowNull: false,
      field: 'role_name'
    },
    roleKey: {
      type: DataTypes.STRING(100),
      allowNull: false,
      field: 'role_key'
    },
    roleSort: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'role_sort',
      get() {
        // 确保返回数字类型
        const value = this.getDataValue('roleSort');
        return value !== undefined ? Number(value) : undefined;
      }
    },
    dataScope: {
      type: DataTypes.CHAR(1),
      defaultValue: '1',
      field: 'data_scope'
    },
    menuCheckStrictly: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'menu_check_strictly'
    },
    deptCheckStrictly: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'dept_check_strictly'
    },
    status: {
      type: DataTypes.CHAR(1),
      allowNull: false,
      defaultValue: '0'
    },
    delFlag: {
      type: DataTypes.CHAR(1),
      defaultValue: '0',
      field: 'del_flag'
    },
    createBy: {
      type: DataTypes.STRING(64),
      defaultValue: '',
      field: 'create_by'
    },
    createTime: {
      type: DataTypes.DATE,
      field: 'create_time'
    },
    updateBy: {
      type: DataTypes.STRING(64),
      defaultValue: '',
      field: 'update_by'
    },
    updateTime: {
      type: DataTypes.DATE,
      field: 'update_time'
    },
    remark: {
      type: DataTypes.STRING(500),
      defaultValue: '',
      field: 'remark'
    },
    // 虚拟字段，不映射到数据库
    flag: {
      type: DataTypes.VIRTUAL,
      defaultValue: false
    },
    menuIds: {
      type: DataTypes.VIRTUAL,
      defaultValue: null
    },
    deptIds: {
      type: DataTypes.VIRTUAL,
      defaultValue: null
    },
    permissions: {
      type: DataTypes.VIRTUAL,
      defaultValue: null
    },
    admin: {
      type: DataTypes.VIRTUAL,
      get() {
        return this.isAdmin();
      }
    }
  },
  {
    sequelize,
    tableName: 'sys_role',
    timestamps: true,
    createdAt: 'createTime',
    updatedAt: 'updateTime',
  }
);

export default Role;